package com.tn.rtdata.examples.stockticker.client;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * A factory class that generates stock ids for use by clients.
 */
public class StockIdFactory
{
  private static final MessageFormat FORMAT_STOCK_ID = new MessageFormat("SK{0}");

  private static List<String> previousStockIds = new ArrayList<String>();

  /**
   * Generates stock ids.
   */
  public static List<String> generateSequentialStockIds(int start, int end)
  {
    if (previousStockIds.size() < end)
    {
      createStockIds(end);
    }

    return new ArrayList<String>(previousStockIds.subList(start, end));
  }

  /**
   * Generates stock ids.
   */
  public static List<String> generateStockIds(int size)
  {
    return createStockIds(size);
  }

  /**
   * Generates stock ids.
   */
  private static List<String> createStockIds(int size)
  {
    List<String> stockIds = new ArrayList<String>();
    while (stockIds.size() < size)
    {
      String stockId = FORMAT_STOCK_ID.format(new Object[]{previousStockIds.size() + 1});
      previousStockIds.add(stockId);
      stockIds.add(stockId);
    }

    return stockIds;
  }
}
